home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 337_01 / l_win1.c < prev    next >
C/C++ Source or Header  |  1991-01-12  |  12KB  |  413 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_WIN1.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <dos.h>
  7.  
  8. #if defined QUICKC
  9.  
  10. #include "malloc.h"
  11. #include "memory.h"
  12.  
  13. #endif
  14.  
  15. #if defined TURBOC
  16.  
  17. #include "alloc.h"
  18. #include "mem.h"
  19. #include "string.h"
  20. #include "stdlib.h"
  21.  
  22. #endif
  23.  
  24.  
  25. /*****************************************************************
  26.  
  27.  Usage: int win_make(int x, int y, int width,int height \
  28.             char *array char *title, char frame_attr,char win_attr);
  29.  
  30.   int x,y =  column,row of upper left corner of active window area
  31.              (not upper left frame corner);
  32.   int width= width of window (interior).
  33.   int height= height of window (interior).
  34.  
  35.   char *array= pointer to string containing frame elements
  36.                (macros defined in mydef.h).
  37.   char *title= title to appear in upper left corner
  38.                of frame.
  39.   char frame_attr=  attribute to use when drawing frame.
  40.   char win_attr= default attribute for window.
  41.  
  42.   Creates a window, returns a "handle" (integer) which is used
  43.   to access the window in later operations.  The newly created
  44.   window is placed on top of all existing windows, and is the
  45.   default active window.  The newly created window is cleared.
  46.   A -1 is returned if too many window are opened.
  47.  
  48.   The window created will be resized and repositioned if necessary
  49.   to fit the screen.
  50.  
  51. *****************************************************************/
  52.  
  53. int win_make(int x,int y,int width,int height,char *array,
  54.              char *title, char frame_attr, char win_attr)
  55. {
  56. extern struct screen_structure scr;
  57. extern struct window_structure w[];
  58.  
  59.  int t,b,l,r;  /* hold calculated values of top,bottom,left,right */
  60.  
  61.    /* check parameters and correct if necessary */
  62.  
  63.    if(scr.ptr==MAX_WINDOW)return(-1);
  64.  
  65.    /* adjust x,y,width & height to fit screen */
  66.  
  67.   if(array[0]=='\0'){                             /* no frame */
  68.  
  69.     if(x<1)x=1;                                   /* too far left? */
  70.     if(y<1)y=1;                                   /* too far up? */
  71.     if(width>scr.columns) width=scr.columns;      /* too wide? */
  72.     if(height>scr.rows) height=scr.rows;          /* too high? */
  73.     if(x+width>scr.columns) x=scr.columns-width+1;/* too far right?*/
  74.     if(y+height>scr.rows) y=scr.rows-height+1;    /* too far down?*/
  75.  
  76.   }
  77.   else{                                        /* frame present */
  78.    if(x<2)x=2;                                 /* allow for frame */
  79.    if(y<2)y=2;
  80.    if(width+2>scr.columns) width=scr.columns-2;   /* too wide? */
  81.    if(height+2>scr.rows) height=scr.rows-2;       /* too high? */
  82.    if(x+width+2>scr.columns) x=scr.columns-width; /* too far right */
  83.    if(y+height+1>scr.rows) y=scr.rows-height;     /* too far down */
  84.   }
  85.  
  86.   if (array[0]=='\0')                       /* no frame */
  87.     w[scr.list[scr.ptr+1]].frame=FALSE;
  88.   else                                      /* frame present */
  89.     w[scr.list[scr.ptr+1]].frame=TRUE;
  90.  
  91.       t=y;b=y+height-1;l=x;r=x+width-1; /* calc. window coordinates*/
  92.  
  93.      win_save();    /* save window (include frame) */
  94.      scr.ptr++;     /* increment screen pointer */
  95.  
  96.      /* make new window the active window */
  97.      scr.active=scr.list[scr.ptr]; 
  98.  
  99.  /* release window so we can write anywhere
  100.     (the printing routines can't write outside existing window
  101.     so we temporarily expand window so we can draw frame)      */
  102.  
  103.   scr.top=1;
  104.   scr.bottom=scr.rows;
  105.   scr.right=scr.columns;
  106.   scr.left=1;
  107.  
  108.   if (array[0]!='\0'){   /* if frame requested */
  109.  
  110.    draw_frame(x-1,y-1,width,height,array,title,frame_attr);
  111.  
  112.   }
  113.  
  114. /* set current attribute to window attribute */
  115. scr.current = win_attr; 
  116.  
  117. w[scr.active].attribute=win_attr;  /* save the window attribute */
  118.  
  119. /* set active window values  */
  120.  
  121. w[scr.active].top=t;w[scr.active].bottom=b; /* save top and bottom */
  122. w[scr.active].left=l;w[scr.active].right=r; /* save left and right */
  123.  
  124. update_margins();
  125.  
  126.     /* set default "underscore" cursor for new window */
  127.  
  128.     if (scr.mode == COLOR_80 || scr.mode == BW_80){
  129.             w[scr.active].start=6; w[scr.active].end=7;
  130.     }
  131.  
  132.     if (scr.mode == MONOCHROME ){
  133.             w[scr.active].start=11;w[scr.active].end = 12;
  134.     }
  135.  
  136.     /* save cursor information */
  137.     cursor(NORMAL_CURSOR);  /* change acutal cursor */
  138.      cls();
  139.  
  140.  
  141. return(scr.active);  /* return the window "handle" */
  142.  
  143. }
  144.  
  145.  
  146. /*****************************************************************
  147.  
  148.  Usage: void win_save(void);
  149.  
  150.  Saves the screen buffer of the top window.
  151.  Used by other routines when the top window is about to be
  152.  over written or the screen is being redrawn.
  153.  
  154.  This function should not be called directly.
  155.  
  156. *****************************************************************/
  157.  
  158. void win_save(void)
  159.  
  160. {
  161. extern struct screen_structure scr;
  162. extern struct window_structure w[];
  163.  
  164.  int y;
  165.  char far *dest_ptr;
  166.  char far *scrn_ptr;
  167.  int buff_size;  /* memory size required to hold saved image */
  168.  int t,b,l,r;
  169.  
  170. /* set t,b,l,r to represent window top,bottom,left and right
  171.    adjust to include frame if necessary */
  172.  
  173.    if (w[scr.active].frame==TRUE){
  174.      t=scr.top-1;b=scr.bottom+1;l=scr.left-1;r=scr.right+1;
  175.     }
  176.     else{
  177.      t=scr.top;b=scr.bottom;l=scr.left;r=scr.right;
  178.     }
  179.    /* copy external screen pointer to local pointer */
  180.    scrn_ptr=scr.buffer; 
  181.  
  182.     if (scr.ptr > MAX_WINDOW){
  183.       cls();puts("too many windows ");exit(1);
  184.       return;
  185.     }
  186.  
  187.     /* calculate memory required to hold screen image */
  188.     buff_size=( (r-l+1)*(b-t+1) )*2;
  189.  
  190.     if (w[scr.active].buffer==NULL)  /* if no memory is allocated */
  191.     w[scr.active].buffer =
  192.       (char  *)malloc (buff_size*sizeof(unsigned char));
  193.  
  194.               /* set up destination pointer (dest_ptr) and
  195.                  screen pointer (scrn_ptr */
  196.  
  197.               dest_ptr=w[scr.active].buffer;
  198.  
  199.               /* initialize pointers */
  200.               scrn_ptr=scr.buffer;
  201.               scrn_ptr=scrn_ptr+((t-1)*(scr.columns*2))+(2*(l-1));
  202.               dest_ptr=w[scr.active].buffer;
  203.  
  204.     /* move each row from screen to buffer */
  205.     for(y=1 ;y<=b-t+1;y++){  
  206.       move_scr_mem(scrn_ptr,dest_ptr,2*(r-l+1));
  207.       scrn_ptr=scrn_ptr +(2*scr.columns); /* update screen pointer */
  208.       dest_ptr=dest_ptr+ (2*(r-l+1));/* update destination pointer */
  209.     }
  210.  
  211. }  /* end function */
  212.  
  213.  
  214. /*****************************************************************
  215.  
  216.  Usage: void win_delete(int handle);
  217.  
  218.  Deletes the window indicated.
  219.  Rearranges window list to reflect change.
  220.  
  221. *****************************************************************/
  222.  
  223. void win_delete(int handle)
  224. {
  225. extern struct screen_structure scr;
  226. extern struct window_structure w[];
  227.  
  228. int location,i;
  229.  
  230. if (handle==0) return;  /* can't delete initial window */
  231.  
  232. /*if it is the top window call win_delete_top */
  233. if (handle==scr.active) {  
  234.       win_delete_top();
  235.       return;
  236. }
  237.  
  238. location=win_validate(handle);
  239. if(location==-1) return;         /* not a window */
  240.  
  241. win_save();  /* save top window */
  242.  
  243. /* Deleting a window requires that the window list be
  244.    rearranged.   We shift all values after the window
  245.    being deleted forward one, then put the deleted
  246.    window at the end      */
  247.  
  248.  
  249. for(i=location;i<scr.ptr+1;i++) scr.list[i]=scr.list[i+1];
  250.  
  251. scr.list[scr.ptr]=handle;  /* move window to top of list */
  252.  
  253. if (w[handle].buffer!=NULL){
  254.  free(w[handle].buffer);  /*free buffer of deleted window */
  255.  w[handle].buffer=NULL;  /* NULL pointer */
  256. }
  257. scr.ptr--;   /* point down one level (we have one less window) */
  258. win_redraw_all();
  259. }
  260.  
  261.  
  262. /*****************************************************************
  263.  
  264.  Usage: int win_validate(int handle);
  265.  
  266.  Checks the list of allocated windows to see if the
  267.  Window handle indicated is valid.
  268.  
  269.  Returns a -1 if not valid, else returns the location
  270.  of the window in the list.
  271.  
  272. *****************************************************************/
  273.  
  274. int win_validate(handle)
  275. {
  276. extern struct  screen_structure scr;
  277. extern